package com.hqu.environment.controller.app;

import com.hqu.environment.annotation.LoginUser;
import com.hqu.environment.dao.ElementStatusDao;
import com.hqu.environment.dao.EquipmentCategoryDao;
import com.hqu.environment.dao.FarmDao;
import com.hqu.environment.dao.MonitorPlanDao;
import com.hqu.environment.dao.NewsDao;
import com.hqu.environment.dao.PlanDetailDao;
import com.hqu.environment.dto.ElementStatusDTO;
import com.hqu.environment.dto.EquipmentCategoryDTO;
import com.hqu.environment.dto.FarmDTO;
import com.hqu.environment.dto.MonitorPlanDTO;
import com.hqu.environment.dto.NewsDTO;
import com.hqu.environment.dto.NewsUserDTO;
import com.hqu.environment.dto.PlanDetailDTO;
import com.hqu.environment.dto.UserFarmDTO;
import com.hqu.environment.entity.Farm;
import com.hqu.environment.enums.NewsTypeEnum;
import com.hqu.environment.utils.Result;
import com.hqu.environment.vo.ElementCheckedVO;
import com.hqu.environment.vo.PlanVO;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 监控方案controller
 * @Date: 2019/04/22 09:17
 * @Author: 邓勇嘉
 **/

@Controller
@RequestMapping("app/plan")
public class PlanController {

    @Autowired
    MonitorPlanDao monitorPlanDao;
    @Autowired
    PlanDetailDao planDetailDao;
    @Autowired
    FarmDao farmDao;
    @Autowired
    EquipmentCategoryDao equipmentCategoryDao;
    @Autowired
    ElementStatusDao elementStatusDao;
    @Autowired
    SqlSession sqlSession;

    @RequestMapping("/getPlanList")
    @ResponseBody
    public Result getPlanList(@LoginUser Integer userId) {
        List<MonitorPlanDTO> monitorPlan = monitorPlanDao.selectListByUserId(userId);
        return Result.success().add("monitorPlan",monitorPlan);
    }


    @RequestMapping("/getPlanById")
    @ResponseBody
    public Result getPlanById(Integer planId) {
        MonitorPlanDTO monitorPlan =new MonitorPlanDTO();
        if(planId!=null){
            monitorPlan = monitorPlanDao.selectByPrimaryKey(planId);
        }
        List<EquipmentCategoryDTO> equipmentCategoryList = equipmentCategoryDao.selectList(null);

        List<ElementCheckedVO> elementCheckedList =new ArrayList<ElementCheckedVO>();
        for(EquipmentCategoryDTO equipmentCategoryItem : equipmentCategoryList){
            Boolean flag =false;
            if(monitorPlan.getPlanDetailList()!=null){
                for(PlanDetailDTO detailItem : monitorPlan.getPlanDetailList()){
                    if (detailItem.getEquipmentCategory() == equipmentCategoryItem.getId()) {
                        flag = true;
                        elementCheckedList.add(new ElementCheckedVO(equipmentCategoryItem.getId(),equipmentCategoryItem.getElement(),equipmentCategoryItem.getUnits(),true,equipmentCategoryItem.getMaxLimit(),equipmentCategoryItem.getMinLimit(),detailItem.getMax(),detailItem.getMin(),detailItem.getValue(),detailItem.getEquipmentCategory()));
                        break;
                    }
                }
            }

            if (!flag)
                elementCheckedList.add(new ElementCheckedVO(equipmentCategoryItem.getId(),equipmentCategoryItem.getElement(),equipmentCategoryItem.getUnits(),false,equipmentCategoryItem.getMaxLimit(),equipmentCategoryItem.getMinLimit(),equipmentCategoryItem.getMaxLimit(),equipmentCategoryItem.getMinLimit(),equipmentCategoryItem.getMinLimit(),equipmentCategoryItem.getId()));
        }

        return Result.success().add("monitorPlan",monitorPlan).add("elementCheckedList",elementCheckedList);
    }

    @RequestMapping("/savePlan")
    @ResponseBody
    public Result savePlan(@LoginUser Integer userId, @RequestBody PlanVO planVO) {
        MonitorPlanDTO monitorPlanDTO =new MonitorPlanDTO();
        BeanUtils.copyProperties(planVO,monitorPlanDTO);
        PlanDetailDao planDetailDaoSqlSession=sqlSession.getMapper(PlanDetailDao.class);
        if(planVO.getId()!=null){
            monitorPlanDao.updateByPrimaryKeySelective(monitorPlanDTO);
            planDetailDao.deleteByPlanId(planVO.getId());
        }else{
            monitorPlanDTO.setCreator(userId);
            monitorPlanDao.insertSelective(monitorPlanDTO);
        }
        for(ElementCheckedVO e : planVO.getElementChecked()){
            if(e.getChecked()){
                PlanDetailDTO p =new PlanDetailDTO();
                p.setPlan(monitorPlanDTO.getId());
                p.setMax(e.getMax());
                p.setMin(e.getMin());
                p.setValue(e.getValue());
                p.setEquipmentCategory(e.getEquipmentCategory());
                planDetailDaoSqlSession.insertSelective(p);
            }
        }
        return Result.success();
    }

    @RequestMapping("/deletePlan")
    @ResponseBody
    public Result deletePlan(Integer planId) {
        monitorPlanDao.deleteByPrimaryKey(planId);
        planDetailDao.deleteByPlanId(planId);
        return Result.success();
    }

    @RequestMapping("/syncFarm")
    @ResponseBody
    public Result syncFarm(Integer planId,Integer farmId) {
        MonitorPlanDTO plan = monitorPlanDao.selectByPrimaryKey(planId);
        List<ElementStatusDTO> elementStatusList = elementStatusDao.selectListByFarmId(farmId);
        List<PlanDetailDTO> planDetailList = plan.getPlanDetailList();

        for(PlanDetailDTO item :planDetailList ){
            for(ElementStatusDTO item2:elementStatusList){
                if (item.getEquipmentCategory()==item2.getEquipmentCategory()){
                    ElementStatusDTO elementStatus =new ElementStatusDTO();
                    elementStatus.setId(item2.getId());
                    elementStatus.setMonitorMax(item.getMax());
                    elementStatus.setMonitorMin(item.getMin());
                    elementStatus.setSettingValue(item.getValue());
                    elementStatusDao.updateByPrimaryKeySelective(elementStatus);

                    break;
                }
            }

        }

        return Result.success();
    }
}
